package org.edu.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.modules.oa.entity.VeFileFilesEntity;
import org.edu.modules.oa.entity.VeOaNoticeEntity;
import org.edu.modules.oa.entity.VeOaWorkPlanEntity;
import org.edu.modules.oa.mapper.*;
import org.edu.modules.oa.service.IVeOaNoticeService;
import org.edu.modules.oa.service.IVeOaWorkPlanService;
import org.edu.modules.oa.vo.VeOaNoticeExportVo;
import org.edu.modules.oa.vo.VeOaNoticeVo;
import org.edu.modules.oa.vo.VeOaWorkPlanExportVo;
import org.edu.modules.oa.vo.VeOaWorkPlanVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class IVeOaWorkPlanServiceImpl extends ServiceImpl<IVeOaWorkPlanMapper, VeOaWorkPlanEntity> implements IVeOaWorkPlanService  {
    @Resource
    private IVeOaWorkPlanMapper iVeOaWorkPlanMapper;
    @Resource
    private IVeFileFilesMapper veFileFilesMapper;

    @Override
    public int addWorkPlan(VeOaWorkPlanEntity veOaWorkPlanEntity, VeFileFilesEntity veFileFilesEntity) {

        int res = 0;
        if (veFileFilesEntity == null) {
            res = iVeOaWorkPlanMapper.insert(veOaWorkPlanEntity);


        }
        if (veFileFilesEntity != null) {
            int add = veFileFilesMapper.insert(veFileFilesEntity);
            if (add != 0) {
                veOaWorkPlanEntity.setFileIds(veFileFilesEntity.getId());

                Integer reust = iVeOaWorkPlanMapper.insert(veOaWorkPlanEntity);
            }
        }

        return res;
    }

    //显示表格
    public HashMap<String, Object> showTable(Integer startPage, Integer pageSize,VeOaWorkPlanEntity veOaWorkPlanEntity) {
        System.out.println("查询条件"+veOaWorkPlanEntity);
        //动态sql模糊查询收阅人
        List<String> names = new ArrayList<>();
        if(!"".equals(veOaWorkPlanEntity.getReaders())) {
         String[] arr = veOaWorkPlanEntity.getReaders().split(",");
            System.out.println(arr[0]);
            for (String s : arr) {
                if(!"".equals(s))
                    names.add(s);
                System.out.println("你模糊查询了几个收阅人"+s);
            }
        }
        List<VeOaWorkPlanVo> voelist = iVeOaWorkPlanMapper.showTable((startPage - 1) * pageSize, pageSize,veOaWorkPlanEntity,names);
        for (VeOaWorkPlanVo veOaWorkPlanVo : voelist) {
             if("".equals(veOaWorkPlanVo.getName())||veOaWorkPlanVo.getName()==null){
                 veOaWorkPlanVo.setName("没有附件");
             }
             if(veOaWorkPlanVo.getReaders()==null||"".equals(veOaWorkPlanVo.getReaders())){
                 veOaWorkPlanVo.setReaders("所有用户");
             }
        }
        int count = iVeOaWorkPlanMapper.tableCount(veOaWorkPlanEntity,names);

        HashMap<String, Object> map = new HashMap<>();
        map.put("list", voelist);
        map.put("count", count);

        // 查询表格总行数
        return map;
    }

    @Override
    public List<VeOaWorkPlanExportVo> xporte() {
        // 拼接查询条件
        QueryWrapper<VeOaWorkPlanEntity> tableWrapper = new QueryWrapper<>();
        tableWrapper.eq("is_deleted",0);

        // 查询加油表的所有数据
        List<VeOaWorkPlanEntity> entitylist =iVeOaWorkPlanMapper.selectList(tableWrapper);

        // 表格集合的VO
        List<VeOaWorkPlanExportVo> voList=new ArrayList<>();
        // 把查出来的东西加入到表格VO里面去
        for(VeOaWorkPlanEntity entity:entitylist){
            VeOaWorkPlanExportVo vo =new VeOaWorkPlanExportVo();
            vo.setId(entity.getId());
            vo.setTitleStr(entity.getTitleStr());
            vo.setContent(entity.getContent());
            vo.setCreateBy(entity.getCreateBy());
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            vo.setCreateTime(formatter.format(entity.getCreateTime()));
            vo.setReaders(entity.getReaders());

            if("".equals(entity.getReaders())||entity.getReaders()==null){
                vo.setReaders("所有用户");
            }

            voList.add(vo);

        }
        return  voList;

    }

    @Override
    public int deleteRecord(Long id) {

        VeOaWorkPlanEntity v= new VeOaWorkPlanEntity();
        v.setId(id);
        v.setIsDeleted(1);
        int r2 = iVeOaWorkPlanMapper.updateById(v);
        System.out.println("删除了" + r2);

        return r2;

    }

    @Override
    public int amendWorkPlan(VeOaWorkPlanEntity veOaWorkPlanEntity, VeFileFilesEntity veFileFilesEntity) throws Exception {

        QueryWrapper<VeOaWorkPlanEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", veOaWorkPlanEntity.getId());
        int reust = 0;
        if (veFileFilesEntity == null) {
            reust = iVeOaWorkPlanMapper.update(veOaWorkPlanEntity, wrapper);
        }
        if (veFileFilesEntity != null) {
            System.out.println("有文件");
            int add = veFileFilesMapper.insert(veFileFilesEntity);

            if (add != 0) {
                veOaWorkPlanEntity.setFileIds(veFileFilesEntity.getId());

                reust = iVeOaWorkPlanMapper.update(veOaWorkPlanEntity, wrapper);
                System.out.println("文件id改了啊"+reust);
            }
        }
        return reust;
    }

}